/*
** initialisation.c for zappy/serveur in /home/Smaxo/Travail/zappy-2015-2014s-renard_g/serv-src
**
** Made by maximilien addari
** Login   <addari_m@epitech.net>
**
** Started on  Wed Jun 13 14:12:20 2012 maximilien addari
** Last update Thu Jul 19 17:47:55 2012 pierrick druart
*/

#include	<unistd.h>
#include	<stdlib.h>
#include	<stdio.h>
#include	<time.h>

#include	"server.h"

static void	drop_objects(t_game *game)
{
  unsigned int	i;
  unsigned int	j;

  i = 0;
  while (i < game->coord.x)
    {
      j = 0;
      while (j < game->coord.y)
	{
	  (game->obj[i][j]).inv[(int)game->drop_rate[rand() % 100]] += 1;
	  j++;
	}
      i++;
    }
}

static void	init_objects(t_game *game)
{
  unsigned int	i;

  i = 0;
  game->obj = xmalloc(sizeof(t_inventory *) * game->coord.x);
  while (i < game->coord.x)
    {
      game->obj[i] = xcalloc(sizeof(t_inventory), game->coord.y);
      i++;
    }
}

static void	init_drop_rate(t_game *game)
{
  int		*conf;
  int		x;
  int		z;
  int		y;

  x = 0;
  z = 0;
  conf = getConf();
  while (x < NB_OBJECT)
    {
      y = 0;
      while (y < conf[x])
	{
	  game->drop_rate[z] = x;
	  y++;
	  z++;
	}
      x++;
    }
  while (z < 100)
    {
      game->drop_rate[z] = FOOD;
      z++;
    }
  free(conf);
}

void		free_obj(t_game *game)
{
  unsigned int	i;

  i = 0;
  while (i < game->coord.x)
    {
      free(game->obj[i]);
      i++;
    }
  free(game->obj);
}

void		init_game(t_game *game)
{
  srand(time(NULL));
  init_drop_rate(game);
  init_objects(game);
  drop_objects(game);
}
